import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    meta: { title: '公寓管理系统', requiresAuth: true },
    component: Home
  },
  {
    path: '/face',
    name: 'Face',
    meta: { title: '人脸识别', requiresAuth: true },
    component: () => import('../views/Face.vue')
  },
  {
    path: '/login',
    name: 'Login',
    meta: { title: '公寓管理系统', requiresAuth: false },
    component: () => import('../views/Login.vue')
  },
  {
    path: '/referring',
    name: 'Referring',
    meta: { title: '注册人脸信息', requiresAuth: true },
    component: () => import('../views/Referring.vue')
  },
  {
    path: '/echart',
    name: 'Echart',
    meta: { title: '数据分析', requiresAuth: true },
    component: () => import('../views/Echart.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth) && !localStorage.getItem('token')) {
    next({
      path: '/login'
    })
  } else {
    next()
  }
})

export default router
